﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common;
using Dln.Common.Helper;
using Dln.Handler;
using Dln.Model;

public partial class Page_AutoUpdate_WeChatStatics : System.Web.UI.Page
{
    protected string 昨日在院人数 = string.Empty; //入院
    protected string 昨日门诊人数 = string.Empty; //出院
    protected string 昨日急诊人数 = string.Empty; //退院
    protected string 昨日总收入 = string.Empty; //在院
    protected string 昨日住院收入 = string.Empty; //在院
    protected string 昨日急诊收入 = string.Empty; //在院
    protected string 昨日门诊收入 = string.Empty; //在院
    protected string 昨日入院人数 = string.Empty; //在院
    protected string 昨日退院人数 = string.Empty; //在院
    protected string 今日在院人数 = string.Empty; //在院
    protected string 今日门诊人数 = string.Empty; //在院
    protected string 今日急诊人数 = string.Empty; //在院
    protected string 今日总收入 = string.Empty; //
    protected string 今日住院收入 = string.Empty; //在院
    protected string 今日急诊收入 = string.Empty; //在院
    protected string 今日门诊收入 = string.Empty; //在院
    protected string 今日入院人数 = string.Empty; //在院
    protected string 今日退院人数 = string.Empty; //在院

    protected bool 昨日在院人数权限 = false;
    protected bool 昨日门诊人数权限 = false;
    protected bool 昨日急诊人数权限 = false;
    protected bool 昨日总收入权限 = false;
    protected bool 昨日住院收入权限 = false;
    protected bool 昨日急诊收入权限 = false;
    protected bool 昨日门诊收入权限 = false;
    protected bool 昨日入院人数权限 = false;
    protected bool 昨日退院人数权限 = false;
    protected bool 今日在院人数权限 = false;
    protected bool 今日门诊人数权限 = false;
    protected bool 今日急诊人数权限 = false;
    protected bool 今日总收入权限 = false;
    protected bool 今日住院收入权限 = false;
    protected bool 今日急诊收入权限 = false;
    protected bool 今日门诊收入权限 = false;
    protected bool 今日入院人数权限 = false;
    protected bool 今日退院人数权限 = false;


    protected string Key = string.Empty;
    protected string UnicodeKey = string.Empty;
    protected string a = String.Empty;

    protected string[] activityRebateType = null;
    protected DateTime ParmeDt = DateTime.Now.Date;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            var searchDate = HttpContext.Current.Request.QueryString["NowDateTime"];
            if (!string.IsNullOrEmpty(searchDate))
            {
                ParmeDt = Convert.ToDateTime(searchDate).Date;
            }
            //var openid = GetAuthOpenId();
            var openid = "oF6dUwxAa6BtWPqaRnE_lz7lDBgQ";
            var weChatUsers = SqlHelper.Context.From<WeChatUsers>().Where(d => d.Openid == openid).ToFirst();
            if (weChatUsers != null)
            {
                var type = weChatUsers.PositionRoles;
                var positionModel = SqlHelper.Context.From<PositionTypes>().Where(d => d.Id == type).ToFirst();
                if (positionModel != null)
                {
                    activityRebateType = positionModel.Interfaces.TrimEnd(',').Split(',');
                }
            }

            Key = "KDNYPYWUMXOW5fEUZ6yUQ1aJx2tgMj5+MgNsqfi6FCcHWsWEAcelgg==";
            UnicodeKey = Key.Replace("+", "%2B");

            string Sql = @"select SUM(typevalue)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'"+ ParmeDt .ToString("yyyy-MM-dd")+ "',112)) and typename   = '在院人数'";
            昨日在院人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select SUM(typevalue)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '门诊人数'";
            昨日门诊人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select SUM(typevalue)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '急诊人数'";
            昨日急诊人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select round(SUM(typevalue),0)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   IN ( '住院费用','急诊费用','门诊费用')";
            昨日总收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '住院费用'";
            昨日住院收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '急诊费用'";
            昨日急诊收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '门诊费用'";
            昨日门诊收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '入院人数'";
            昨日入院人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   dateadd(day,-1,CONVERT (nvarchar(12),'" + ParmeDt.ToString("yyyy-MM-dd") + "',112)) and typename   = '退院人数'";
            昨日退院人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '在院人数'";
            今日在院人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '门诊人数'";
            今日门诊人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '急诊人数'";
            今日急诊人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select SUM(round(typevalue,0))  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename in ('住院费用','急诊费用','门诊费用')";
            今日总收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '住院费用'";
            今日住院收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '急诊费用'";
            今日急诊收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '门诊费用'";
            今日门诊收入 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0.00");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '入院人数'";
            今日入院人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");

            Sql = @"select round(typevalue,0)  from  HIS_CURRVALUE where Currdate  =   CONVERT (nvarchar(12),GETDATE(),112) and typename   = '退院人数'";
            今日退院人数 = Convert.ToDouble(SqlHelper.Context.FromSql(Sql).ToScalar() + "").ToString("0");


            //昨日在院人数权限
            var interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日在院人数").ToFirst();
            if (interfacesModel != null)
            {
                昨日在院人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日门诊人数").ToFirst();
            if (interfacesModel != null)
            {
                昨日门诊人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日急诊人数").ToFirst();
            if (interfacesModel != null)
            {
                昨日急诊人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
               SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日总收入").ToFirst();
            if (interfacesModel != null)
            {
                昨日总收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日住院收入").ToFirst();
            if (interfacesModel != null)
            {
                昨日住院收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日急诊收入").ToFirst();
            if (interfacesModel != null)
            {
                昨日急诊收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日门诊收入").ToFirst();
            if (interfacesModel != null)
            {
                昨日门诊收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日入院人数").ToFirst();
            if (interfacesModel != null)
            {
                昨日入院人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "昨日退院人数").ToFirst();
            if (interfacesModel != null)
            {
                昨日退院人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }
            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日在院人数").ToFirst();
            if (interfacesModel != null)
            {
                今日在院人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日门诊人数").ToFirst();
            if (interfacesModel != null)
            {
                今日门诊人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日急诊人数").ToFirst();
            if (interfacesModel != null)
            {
                今日急诊人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日总收入").ToFirst();
            if (interfacesModel != null)
            {
                今日总收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
                SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日住院收入").ToFirst();
            if (interfacesModel != null)
            {
                今日住院收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
               SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日急诊收入").ToFirst();
            if (interfacesModel != null)
            {
                今日急诊收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
               SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日门诊收入").ToFirst();
            if (interfacesModel != null)
            {
                今日门诊收入权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
               SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日入院人数").ToFirst();
            if (interfacesModel != null)
            {
                今日入院人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

            interfacesModel =
              SqlHelper.Context.From<InterfacesTypes>().Where(d => d.InterfaceName == "今日退院人数").ToFirst();
            if (interfacesModel != null)
            {
                今日退院人数权限 = activityRebateType.Contains<string>(interfacesModel.Id.ToString());
            }

        }
        catch (Exception ex)
        {
            Response.Redirect("/Message.aspx?ex=" + ex, true);
        }


    }

    public string GetAuthOpenId()
    {
        string AppId = CommonHelper.GetAppSettings("AppID");
        string AppSecret = CommonHelper.GetAppSettings("AppSecret");
        string strOpenId = "";
        string name = "";
        string code = DNTRequest.GetString("CODE");

        var cookies = HttpContext.Current.Request.Cookies["openidcookie"];
        if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
        {
            strOpenId = cookies.Value;
        }
        else
        {
            if (!String.IsNullOrEmpty(code))
            {
                string json = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", AppId, AppSecret, code));
                if (!String.IsNullOrEmpty(json))
                {
                    try
                    {
                        OAuthConfig oAuthConfig = JSONHelper.FromJson<OAuthConfig>(json);
                        if (oAuthConfig != null)
                        {
                            HttpCookie cookie = new HttpCookie("openidcookie");
                            cookie.Value = oAuthConfig.openid;
                            cookie.Expires = DateTime.Now.AddDays(2);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            strOpenId = oAuthConfig.openid;
                        }

                        else
                        {
                            strOpenId = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        strOpenId = "";
                    }
                }
            }
            else
            {
                var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2FPage%2FAutoUpdate%2FWeChatStatics.aspx&response_type=code&scope=snsapi_base&state=test#wechat_redirect", AppId, CommonHelper.GetAppSettings("mainurl").Replace(@"://", "%3A%2F%2F"));
                Response.Redirect(url);

            }
        }
        return strOpenId;
    }

    public void ResponseToPage(string message)
    {
        Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode(message)));
    }
}